<script setup lang="ts">
//
import CustomNavbar from './components/CustomNavbar.vue'
import XtxSwiper from './components/XtxSwiper.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import { ref } from 'vue'
import { getHomeBanner, getHomeCategoryMutil, getHomeHotMutil } from '@/services/home'
import HotPanel from './components/HotPanel.vue'
import type { XtxGuessInstance } from '@/types/component'
import XtxGuess from '@/components/XtxGuess.vue'
// 获取首页广告区域数据
const banners = ref<BannerItem[]>([])
// 获取首页热门推荐数据
const hotList = ref<HotItem[]>([])
const getBanners = async () => {
  const res = await getHomeBanner()
  banners.value = res.result
}

getBanners()
// 获取首页前台类目数据
const category = ref<CategoryItem[]>([])
const getCategory = async () => {
  const res = await getHomeCategoryMutil()
  category.value = res.result
}
getCategory()
// 获取首页热门推荐数据
const getHotList = async () => {
  const res = await getHomeHotMutil()
  console.log('首页热门推荐数据', res)
  hotList.value = res.result
}
getHotList()

// 猜你喜欢组件实例
const gussRef = ref<XtxGuessInstance>()
// 滚动触底事件
const onScrollLower = () => {
  gussRef.value?.getMore()
}

// 下拉刷新状态
const isTriggered = ref(false)
// 自定义下拉刷新触发
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢租组件数据
  gussRef.value?.resetData()

  await Promise.all([getBanners(), getCategory(), getHotList()])
  // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <view class="index">
    <!-- 导航栏 -->
    <CustomNavbar />
    <!-- 轮播图 -->
    <XtxSwiper :list="banners" />
    <!-- 分类 -->
    <CategoryPanel :list="category" />
    <!-- 热门推荐 -->
    <HotPanel :hotList="hotList" />
    <!-- 滚动容器 -->
    <scroll-view
      scroll-y
      class="scroll-view"
      @scrolltolower="onScrollLower"
      refresher-enabled
      @refresherrefresh="onRefresherrefresh"
      :refresher-triggered="isTriggered"
    >
      <!-- 猜你喜欢 -->
      <XtxGuess ref="gussRef" />
    </scroll-view>
  </view>
</template>

<style lang="scss">
//
</style>
